﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GameEngine.PDNParser;

namespace Daphne.Forms
{
    /// <summary>
    /// Interaction logic for GameSelectForm.xaml
    /// </summary>
    public partial class GameSelectForm : Window
    {
        public GameSelectForm()
        {
            InitializeComponent();

            uxCancel.Click += new RoutedEventHandler(uxCancel_Click);
            uxLoad.Click += new RoutedEventHandler(uxLoad_Click);
        }

        public int SelectedGameIndex
        { get; set; }

        void uxLoad_Click(object sender, RoutedEventArgs e)
        {
            object obj = uxGameList.SelectedItem;
            if (obj == null || !(obj is Item))
            {
                return;
            }

            Item item = obj as Item;
            int index;
            int.TryParse(item.Index, out index);
            SelectedGameIndex = index;
            
            this.DialogResult = true;
            this.Close();
        }

        void uxCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        public void LoadGames(PDNRawGameList gameList)
        {
            List<Item> items = new List<Item>();

            int index = 0;
            foreach (var game in gameList.Games)
            {
                Item item = new Item();
                item.Index = index.ToString();
                item.Event = GetHeader(game, PDNHeaderKey.Event);
                item.Site = GetHeader(game, PDNHeaderKey.Site);
                item.White = GetHeader(game, PDNHeaderKey.White);
                item.Black = GetHeader(game, PDNHeaderKey.Black);
                item.Round = GetHeader(game, PDNHeaderKey.Round);
                item.Result = GetHeader(game, PDNHeaderKey.Result);

                index++;
                items.Add(item);
            }

            this.DataContext = items;
            uxGameList.SelectedIndex = 0;
        }

        private string GetHeader(PDNRawGame game, string headerName)
        {
            foreach (var header in game.Headers)
                if (header.Key == headerName)
                    return header.Value;

            return string.Empty;         
        }

        private class Item
        {
            public string Index
            { get; set; }

            public string Event
            { get; set; }

            public string Site
            { get; set; }

            public string White
            { get; set; }

            public string Black
            { get; set; }

            public string Round
            { get; set; }

            public string Result
            { get; set; }
        }


    }
}
